﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.AfterSaleCarKPI;

namespace S4Store.Controllers
{
    public class AfterSaleCarKPIController : ReportBaseController
    {
        //
        // GET: /AfterSaleCarKPI/
        public CarService CarService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }

        public ActionResult Index()
        {
            AfterSaleCarTypeIndexModel model = new AfterSaleCarTypeIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var data = AfterSaleCarKPIService.GetAfterSaleCarKpiDayDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult MonthTd(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var config = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(y, m, Store.Id).FirstOrDefault();
                if (config == null)
                {
                    config = new Domain.AfterSaleKpi.AfterSaleCarTypeMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }

        public ActionResult InputCarKpiMonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarTypeListModel model = new AfterSaleCarTypeListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.AfterSaleCarModelMonthConfigs = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(y, m, Store.Id);
                model.Date = new DateTime(y, m, 1);
                model.isconfirmed = model.AfterSaleCarModelMonthConfigs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public ActionResult AfterSaleCarModelList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                AfterSaleCarTypeListModel model = new AfterSaleCarTypeListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                return View(model);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="afterSaleCarlist"></param>
        /// <returns></returns>
        public JsonResult ModifyBackInputAfterSaleCarTypeKpiMonthConfig(IList<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeMonthConfig> afterSaleCarlist,int y,int m, bool isconfirm=false)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, 1);
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeMonthConfig>();
                int sid = CurrentUser.StoreId.Value;
                if (CurrentUser.Position == Position.SystemManager)
                {
                    sid = Store.Id;
                }
                foreach (var model in afterSaleCarlist)
                {
                    model.Month = now;
                    model.StoreId = sid;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitAfterSaleCarTypeMonthConfigList(now.Year, now.Month, sid, list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

        public JsonResult ModifyBackInputAfterSaleCarTypeKpiDayData(IList<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeDayInput> afterSaleCarDayList, int y,int m,int d, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, d);
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeDayInput>();
                int sid = CurrentUser.StoreId.Value;
                if (CurrentUser.Position == Position.SystemManager)
                {
                    sid = Store.Id;
                }
                foreach (var model in afterSaleCarDayList)
                {
                    model.AddTime = now;
                    model.StoreId = sid;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitAfterSaleCarTypeDayDataList(now.Year, now.Month, now.Day, sid, list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

        public ActionResult InputCarKpiDayData(int y, int m, int d)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarTypeListModel model = new AfterSaleCarTypeListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.Date = new DateTime(y, m, d);
                model.AfterSaleCarTypeDayInputs = AfterSaleCarKPIService.GetAfterSaleCarTypeDayInputs(y, m, d, Store.Id);
                model.isconfirmed = model.AfterSaleCarTypeDayInputs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

    }
}
